﻿//module.exports= function (callback, name) {

//    let  msg = "msg" + name;

//    callback(null,msg)
//}

//module.exports = function (callback, name) {
//    var msg = 'Hello,' + name;
//    callback(null, msg);
//};



//var pdf = pdfmake.createPdf(docDefinition);
//pdf.write('pdfs/qrCode.pdf').then(() => {
//	console.log(new Date() - now);
//}, err => {
//	console.error(err);
//});

//module.exports = function (callback) {


//	//var pdfmake = require('pdfmake');
//	var greeting = 'Can you see me';
//	var url = 'http://pdfmake.org';
//	var longText = 'The amount of data that can be stored in the QR code symbol depends on the datatype (mode, or input character set), version (1, …, 40, indicating the overall dimensions of the symbol), and error correction level. The maximum storage capacities occur for 40-L symbols (version 40, error correction level L):';


//	function header(text) {
//		return { text: text, margins: [0, 0, 0, 8] };
//	}

//	var dd = {
//		pageMargins: [10, 10, 10, 10],
//		content: [
//			header(greeting),
//			{ qr: greeting },
//			'\n',

//			header('Colored QR'),
//			{ qr: greeting, foreground: 'red', background: 'yellow' },
//			'\n',

//			header(url),
//			{ qr: url },
//			'\n',

//			header('A very long text (' + longText.length + ' chars)'),
//			{ qr: longText },
//			'\n',
//			header('same long text with fit = 100 and alignment = right'),
//			{ qr: longText, fit: 150, alignment: 'right' },
//		]
//	};

//	var now = new Date();


//   var doc=	createPdfBinary(dd, x => {

//   });

//	var st = doc.save

//	callback(null, st);




//	function createPdfBinary(docDefinition,callback) {


//		var pdfmake = require('pdfmake/build/pdfmake'); // only during development, otherwise use the following line

//		var PDF_Fonts = require('pdfmake/build/vfs_fonts');


//		pdfmake.vfs = PDF_Fonts.pdfMake.vfs;

//		var pdf = pdfmake.createPdf(docDefinition);
//		return pdf.getStream(callback);
//	}
//	// callback(null,b)
//}



const path = require('path');
const pdfMakePrinter = require('pdfmake/src/printer');

module.exports = function (callback, name, templateName) {
    let dd = require('./templates/' + templateName)(name);
    createPdfBinary(dd, function (base64) {
        callback(null, base64);
    });
};

function createPdfBinary(pdfDoc, callback) {
    var fontDescriptors = {
        Roboto: {
            normal: path.join(__dirname, './fonts/Roboto-Regular.ttf'),
            bold: path.join(__dirname, './fonts/Roboto-Medium.ttf'),
            italics: path.join(__dirname, './fonts/Roboto-Italic.ttf'),
            bolditalics: path.join(__dirname, './fonts/Roboto-MediumItalic.ttf')
        }
    };

    var printer = new pdfMakePrinter(fontDescriptors);
    var doc = printer.createPdfKitDocument(pdfDoc);

    var chunks = [];
    var result;

    doc.on('data', function (chunk) {
        chunks.push(chunk);
    });
    doc.on('end', function () {
        result = Buffer.concat(chunks);

        // return PDF as byte[] 
        // callback(result);

        // return PDF as Base64 string
        callback(result.toString('base64'));
    });
    doc.end();
}